import {PermissionApi, UserApi_login, UserApi_logout} from "@/api/api";
import {RECEIVE_TOKEN} from "@/api/config";
import store from "@/store/store"

import {getJson} from "@/api/http";

export function storeToken(token) {
    store.commit(RECEIVE_TOKEN, {
        token: token
    })
}

/*登录*/
export async function userLogin(data) {
    let res = await getJson(UserApi_login + "/" + 0, data);
    if (res.code === 1) {
        let token = res.data
        storeToken(token)
    }
    return res
}

// 更新用户信息 -> 更新token -> 更新userInfo
export async function updateUser(data) {
    // let res = await putJson(UserApi, data.id, data)
    // todo
    // let token = res.data
    // storeToken(token)
}

// 登出
export async function userLogout() {
    await getJson(UserApi_logout + "/" + 0, {});// 单点登录
    storeToken("")
}


export async function userRegister(data) {
    // let res = await postJson2(UserApi, 0, data);
    // return res.data
}


export async function hasPermission(userId, permission) {
    let data = {permission: permission}
    let res = await getJson(PermissionApi + "/" + userId, data);
    if (res.code === 1) {
        return res.data
    }
    return false
}

